import { Thing } from "./thing";
import { Scene, PlaneGeometry, TextureLoader, RepeatWrapping, Vector3 } from 'three';
import { Water } from 'three/examples/jsm/objects/Water';
import { Sun } from "./sun";
import { TWEEN } from "three/examples/jsm/libs/tween.module.min.js";

export class WaterPlane extends Thing{

    water:Water;

    constructor(scene: Scene, sun: Sun) {
        super(scene, sun);
        // 水
        const waterGeometry = new PlaneGeometry(10000, 10000);
        let fog:any = false;
        if (this._scene) {
            fog = this._scene.fog;
        }

        this.water = new Water(
            waterGeometry,
            {
                textureWidth: 512,
                textureHeight: 512,
                waterNormals: new TextureLoader().load('./textures/waternormals.jpg', function (texture) {
                    texture.wrapS = texture.wrapT = RepeatWrapping;
                }),
                sunDirection: new Vector3(),
                sunColor: 0xffffff,
                waterColor: 0x001e0f,
                distortionScale: 3.7,
                fog: fog
            }
        );

        this.water.rotation.x = - Math.PI / 2;
        this.water.material.uniforms['time'].value += 1.0 / 60.0;

        this._scene?.add(this.water);

    }

    update() {

        this.water.material.uniforms['time'].value += 1.0 / 60.0;
        if (this.water) {
            this.water.material.uniforms['sunDirection'].value.copy(this._sun.vector).normalize();
        }
        TWEEN.update()
    }

}